package com.my.basic.init;

/**
 * @author marshal
 * @version 2021-06-01
 */
public class InitialBlock {
    private String name;
    private int age;

    /**
     * final修饰的变量必须在声明时就显式赋值或者在构造器里赋值
     */
    private final int a; // 在构造器赋值

    private final int b = 20; // 声明时显式赋值

    static {
        System.out.println("This is static initial block!");
    }

    {
        name = "Jack";
        System.out.println("This is initial block!");
    }

    {
        name = "Tom";
        System.out.println("This is second initial block!");
    }

    public InitialBlock() {
        a = 10;
        System.out.println("This is constructor!");
    }

    public static void main(String[] args) {
        InitialBlock block = new InitialBlock();
        System.out.println(block);
    }


    @Override
    public String toString() {
        return "InitialBlock{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
